///////////////////////////////////////////////////////////////////////////////////
// 附件调用接口封装
//开发人：虞胜强
//开发时间：2019/3/7
///////////////////////////////////////////////////////////////////////////////////
layui.define(['jquery','dialogUtil','upload','baseUtil','checkUtil'],function(exports){
    var $ = layui.jquery,
        dialogUtil = layui.dialogUtil,
        checkUtil = layui.checkUtil,
        upload = layui.upload,
        baseUtil = layui.baseUtil;


    var fileUtil = {
        /**
         * 附件查询接口
         *
         * @param objectId 附件objectId
         * @param successFn 成功回调
         * @author shengQiang yu
         * @date 2019-03-07 11:25
         */
        acceptFile: function (objectId, successFn) {
            baseUtil.postMethod("/sysFile/list", {objectId: objectId}, successFn);
        },
        /**
         * 单张图片
         *
         * @param objectId 绑定的对象Id
         * @param catalog 上传图片目录位置
         * @param objectValue 默认object值
         */
        uploadImageNoSave: function (objectId,catalog,path) {
            //第一步：初始化数据
            var htmlContent='<input type="hidden" name="'+objectId+'" value="'+path+'" />';
            if(checkUtil.isEmpty(path)){
                htmlContent+='<img src="" style="display: none;width: 100%;height: 100%;"/>'+
                    '<div class="btnctn">'+
                    '<i class="iconfont" style="font-size: 88px;">&#xe601;</i><br>'+
                    '</div>';
            }else{
                htmlContent+='<img src="'+path+'" style="width: 100%;height: 100%;"/>'+
                    '<div class="btnctn" style="display: none;">'+
                    '<i class="iconfont" style="font-size: 88px;">&#xe601;</i><br>'+
                    '</div>';
            }
            var elem=$("#"+objectId);
            elem.html(htmlContent);
            //第二步：设置事件
            upload.render({
                elem:elem //绑定元素
                , url: '/sysFile/singleFile' //上传接口
                , data:  {catalog: catalog, requestFileType:"1",isSave:"2"}
                , done: function (result) {
                    if (0 == result.code) {
                        var imgObj = $(elem).find("img");
                        imgObj.attr("src", result.data);
                        $("#"+objectId+" input[name='" +objectId+ "']").val(result.data);
                        imgObj.show();
                        $("#"+objectId+" .btnctn").hide();
                    } else {
                        dialogUtil.msg(result.msg);
                    }
                }
                , error: function () {
                    //请求异常回调
                    dialogUtil.msg("请求异常");
                }
            });
        },
        /**
         * 多图片
         *
         * @param objectId 绑定的对象Id
         * @param catalog 上传图片目录位置
         * @param objectValue 默认object值
         */
        uploadMutiImageNoSave: function (objectId,catalog,paths) {
            //第一步：初始化数据
            var htmlContent='<input type="hidden" name="'+objectId+'" value="'+paths+'" />';
            htmlContent+='<button type="button" class="layui-btn" id="'+objectId+'Image">立即上传</button>&nbsp;&nbsp;<span style="color: red;">图片宽为750px,高为320px。' +
                '<a target="_blank" href="https://tinypng.com/">立即压缩</a> </span>'+
                '<blockquote class="layui-elem-quote layui-quote-nm" id="'+objectId+'ImageContent" style="margin-top: 10px; min-height: 100px;">';
            if(paths!=null&&paths!=""){
                var imageList=paths.split(",");
                for(var i=0;i<imageList.length;i++){
                    htmlContent+='<div class="xyg-upload-item" style="font-size: 10px">'+
                        '<img src="'+imageList[i]+'"  class="xyg-upload-item-pic"  data-url="'+imageList[i]+'" style="width: 90px; height: 90px;"/>'+
                        '<div class="xyg-upload-item-btn">'+
                        '<span class="xyg-upload-item-btn-xz">向左</span>&nbsp;'+
                        '<span class="xyg-upload-item-btn-xy">向右</span>'+
                        '<span class="xyg-upload-item-btn-right">删除</span>'+
                        '</div>'+
                        '</div>';
                }
            }
            htmlContent+='<div style="clear: both;"></div></blockquote>';

            var elem=$("#"+objectId);
            elem.html(htmlContent);
            //第二步：设置事件
            upload.render({
                elem: '#'+objectId+'Image'
                , url: '/sysFile/singleFile' //上传接口
                , data:  {catalog: catalog, requestFileType:"1",isSave:"2"}
                , done: function (result) {
                    if (0 == result.code) {
                        var str='<div class="xyg-upload-item">'+
                            '<img src="'+result.data+'" class="xyg-upload-item-pic" data-url="'+result.data+'" style="width: 90px; height: 90px;" />'+
                            '<div class="xyg-upload-item-btn" style="font-size: 10px">'+
                            '<span class="xyg-upload-item-btn-xz">向左</span>&nbsp'+
                            '<span class="xyg-upload-item-btn-xy">向右</span>'+
                            '<span class="xyg-upload-item-btn-right">删除</span>'+
                            '</div>'+
                            '</div>';
                        $('#'+objectId+'ImageContent').append(str);
                        var v="";
                        elem.find(".xyg-upload-item img").each(function(index,obj){
                            if(index==0){
                                v=$(obj).attr("src");
                            }else{
                                v+=","+$(obj).attr("src");
                            }
                        });
                        elem.find("input[name='"+objectId+"']").val(v);
                    } else {
                        dialogUtil.msg(result.msg);
                    }
                }
                ,multiple: true
                ,before: function(obj){

                }
            });

            elem.on('click','.xyg-upload-item .xyg-upload-item-pic',function(){
                fileUtil.imageSingleShow($(this).attr("data-url"));
            });
            elem.on('click','.xyg-upload-item .xyg-upload-item-btn-xz',function(){
                var obj=$(this).parent().parent();
                var prev = obj.prev();
                if(prev.length>0){
                    prev.before(obj.prop("outerHTML"));
                    obj.remove();
                    var v="";
                    elem.find(".xyg-upload-item img").each(function(index,obj){
                        if(index==0){
                            v=$(obj).attr("src");
                        }else{
                            v+=","+$(obj).attr("src");
                        }
                    });
                    elem.find("input[name='"+objectId+"']").val(v);
                }
            });
            elem.on('click','.xyg-upload-item .xyg-upload-item-btn-xy',function(){
                var obj=$(this).parent().parent();
                var next = obj.next();
                if(next.length>0){
                    next.after(obj.prop("outerHTML"));
                    obj.remove();
                    var v="";
                    elem.find(".xyg-upload-item img").each(function(index,obj){
                        if(index==0){
                            v=$(obj).attr("src");
                        }else{
                            v+=","+$(obj).attr("src");
                        }
                    });
                    elem.find("input[name='"+objectId+"']").val(v);
                }
            });
            elem.on('click','.xyg-upload-item .xyg-upload-item-btn-right',function(){
                var obj=$(this);
                dialogUtil.confirm("确定删除图片吗？",function () {
                    obj.parent().parent().remove();
                    var v="";
                    elem.find(".xyg-upload-item img").each(function(index,obj){
                        if(index==0){
                            v=$(obj).attr("src");
                        }else{
                            v+=","+$(obj).attr("src");
                        }
                    });
                    elem.find("input[name='"+objectId+"']").val(v);
                });
            });
        },
        /**
         * 单张图片显示（用于单图片上传）
         *
         * @param elem 操作对象
         * @param showObj 图片展示内容
         * @param data 图片数据
         */
        uploadImageShow: function (elem,showObj,data) {
            var imgObj = $(elem).find("img");
            imgObj.attr("src", data.path);
            showObj.val(data.objectId);
            imgObj.show();
        },
        /**
         * 单张图片展示
         *
         * @param path 路径
         */
        imageSingleShow: function (path) {
            var data=[{
                "alt": "",
                "pid":1, //图片id
                "src": path, //原图地址
                "thumb": "" //缩略图地址
            }];
            fileUtil.imageBaseShow('',baseUtil.getRandom(6),0,data);
        },/**
         * 基础图片展示
         *
         * @param title 相册标题
         * @param id 相册id
         * @param start 初始显示的图片序号，默认0
         * @param data 图片数据，格式
         * [{
             "alt": "图片名",
             "pid": 666, //图片id
              "src": "", //原图地址
              "thumb": "" //缩略图地址
             }
           ]
         */
        imageBaseShow: function (title,id,start,data) {
            var json={
                "title":title,
                "id":id, //相册id
                "start":start, //
                "data": data
            };
            layer.photos({
                photos: json
                ,shift: 5 //0-6的选择，指定弹出图片动画类型，默认随机
            });
        }

    };
    //输出baseUtil接口
    exports('fileUtil', fileUtil);
});

